var P2 = function (game) {

  this.create = function () {
    ClosePage(game,'2_0','2','2_r','2_w');

    function Page(game,cbg,mbg,rbg,wbg){
      var origin = [[550, 550], [780, 550]];
      var rectPos = [[900, 360], [1200, 360],[900,470],[1200,470]];
      var flagAry=[2,2,2,2];
      var container=game.add.sprite(0,0,'');
      game.add.sprite(0, 0, mbg);

      var img;
      var group=game.add.group();
      for(var i=0;i<4;i++){
        img = game.add.sprite(rectPos[i][0], rectPos[i][1], '');
        img.name=i;
        group.addAt(img,i);
      }


      function showWr() {
        for (var i = 0; i < 4; i++) {
          var obj = group.getChildAt(i);
          if (flagAry[i] === 0) {
            obj.loadTexture('1w0');
          } else if (flagAry[i] === 1) {
            obj.loadTexture('1r0');
          }
        }
      }

      var rectGroup=game.add.group();
      var rectBound;
      for(var i=0;i<4;i++){
        if(i === 0){
            rectBound = game.add.sprite(500, 170,'' );
            rectBound.width=450;
            rectBound.height=230;
        }
        if(i === 1){
            rectBound = game.add.sprite(950, 170,'' );
            rectBound.width=450;
            rectBound.height=230;
        }
        if(i === 2){
            rectBound = game.add.sprite(500, 410,'' );
            rectBound.width=450;
            rectBound.height=130;
        }
        if(i === 3){
            rectBound = game.add.sprite(950, 410,'' );
            rectBound.width=450;
            rectBound.height=130;
        }

          rectBound.data=i;

        rectGroup.add(rectBound);
      }


      Wbtn=game.add.button(550, 550, '1w', '', this, 2, 1, 0);
      Wbtn.data=0;
      Rbtn=game.add.button(780, 550, '1r', '', this, 2, 1, 0);
      Rbtn.data=1;

      //container.addChild(Wbtn);
      //container.addChild(Rbtn);

      Wbtn.inputEnabled = true;
      Wbtn.input.enableDrag(false, true);
      Wbtn.events.onDragStop.add(fixLocation);

      Rbtn.inputEnabled = true;
      Rbtn.input.enableDrag(false, true);
      Rbtn.events.onDragStop.add(fixLocation);



      function fixLocation(item) {
        var index=10;
        var bounds = item.getBounds();
        rectGroup.children.forEach(function (obj,i) {
          var boundsB = obj.getBounds();

          //var p=new Phaser.Point(bounds.x+bounds.width/2,bounds.y+bounds.width/2);
          var p=new Phaser.Point(bounds.centerX,bounds.centerY);
          var isIn=Phaser.Rectangle.containsPoint(boundsB, p);
         // console.log(bounds,bounds.centerX,bounds.centerY)
          //console.log(isIn);
          //var flag = Phaser.Rectangle.intersects(bounds, boundsB);
          if(isIn){

            flagAry[obj.data]=item.data;
            index=i;

            showWr();
            return;
          }
            tween = game.add.tween(item).to( { x:origin[item.data][0],y: origin[item.data][1] }, 10, Phaser.Easing.Linear.None, true);
        });
      }


      var retry = game.add.button(1080, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      retry.name = 'retry';
      retry.width = 160;
      retry.height = 55;
      //container.addChild(retry);

      var submit = game.add.button(1250, 625, imgs['button'], onSubmit, this, 2, 1, 0);
      submit.name='submit';
      submit.width = 160;
      submit.height = 55;
      //container.addChild(submit);

      function onSubmit(arg) {
        container.destroy();
        if(arg.name === 'submit') {
          if (flagAry.join('_') === '0_0_0_1') {
            RightPage(game, cbg, mbg, rbg, wbg);
          } else {
            WrongPage(game, cbg, mbg, rbg, wbg);
          }
        }else{
          game.goState('back');
        }
      }
    }

    function ClosePage(game,cbg,mbg,rbg,wbg) {
      var container=game.add.sprite(0,0,'');
      var close = game.add.button(0, 0, cbg, onClose, this, 2, 1, 0);
      container.addChild(close);
      function onClose() {
        container.destroy();
        Page(game,cbg,mbg,rbg,wbg);
      }
    }
    function WrongPage(game,cbg,mbg,rbg,wbg){
        var container=game.add.sprite(0,0,'');

        var bg = game.add.image(0, 0, wbg);
        container.addChild(bg);
        var retry = game.add.button(1250, 625, imgs['button'], onClick, this, 2, 1, 0);
        retry.name = 'retry';
        retry.width = 160;
        retry.height = 55;
        container.addChild(retry);

        function onClick() {
            container.destroy();
            Page(game, cbg, mbg, rbg, wbg);
        }
    }

    function RightPage(game,cbg,mbg,rbg,wbg){
      var container=game.add.sprite(0,0,'');

      var bg = game.add.image(0, 0, rbg);
      container.addChild(bg);
      var next = game.add.button(1250, 625, imgs['button'], onClick, game, 2, 1, 0);
      next.width = 160;
      next.height = 55;
      container.addChild(next);

      function onClick() {
        game.goState('next');
      }
    }
  }

}





